package com.controller.selling 
{
	import com.model.valueObjects.VoPurchaseOrder;
	import com.model.valueObjects.VoSalesConformation;
	import com.service.SalesConformationMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class SalesConformationController extends AbstractController
	{
	
		[Inject]
		public var salesConformationDetails:SalesConformationMaster;
		
		public function SalesConformationController()
		{
			super();
		}
		
		[EventHandler(event="SalesConformationEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(salesConformationDetails.getAllSalaesConformation(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{				
			getAllStock();
			
			if(model.currentMenu=='scBtn')
				model.currentSellTableIndex=1;
			
			model.salesConformationList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		[EventHandler(event="SalesConformationEvent.GET_ALL_HEADERS")]
		public function getAllheaders():void{
			sh.executeServiceCall(salesConformationDetails.getAllSCHeaderList(),getAllHeaderResult, handleFault );
		}
		private function getAllHeaderResult(event:ResultEvent):void
		{	
			model.scHeaderList = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		
		[EventHandler(event="SalesConformationEvent.GET_ALL_FOOTERS")]
		public function getAllFooters():void{
			sh.executeServiceCall(salesConformationDetails.getAllScFooterPopupList(),getAllFooterResult, handleFault );
		}
		private function getAllFooterResult(event:ResultEvent):void
		{	
			model.scFooterList = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
		}
		
		[EventHandler(event="SalesConformationEvent.GET_ALL_FREE_ALLOCATED",properties="pro_serialnumber")]
		public function getAllFreeAllocated(pro_serialnumber:int):void{
			sh.executeServiceCall(salesConformationDetails.getFreeAllocationQty(pro_serialnumber),getAllFreeAllocatedResult, handleFault );
		}
		
		private function getAllFreeAllocatedResult(event:ResultEvent):void
		{					
			model.poFreeAloocation = event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="SalesConformationEvent.GET_ALL_STOCK")]
		public function getAllStock():void{
			sh.executeServiceCall(salesConformationDetails.getAllStockOnSc(),getAllStockResult, handleFault );
		}
		
		private function getAllStockResult(event:ResultEvent):void
		{
			model.stockList=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="SalesConformationEvent.GET_SC_ITEMS",properties="customerFk")]
		public function getScItems(customerFk:Number):void{
			sh.executeServiceCall(salesConformationDetails.getItemsForDo(customerFk),getScItemsResult, handleFault );
		}
		
		private function getScItemsResult(event:ResultEvent):void
		{
			model.scItemsForDo=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="SalesConformationEvent.ADD_WITH_PO", properties="salesConformationvo,poVo")]
		public function addWithPo(salesConformationvo:VoSalesConformation,poVo:VoPurchaseOrder):void
		{
			sh.executeServiceCall(salesConformationDetails.addNewIndentSalesConformationWithPO(salesConformationvo,poVo), addWithPoResult, handleFault );	
		}
		
		private function addWithPoResult(event:ResultEvent):void
		{
			var sc_code:String = event.result as String;
			
			if(model.saveCloseBtn)model.currentSellFormsIndex=0;
			
			model.scStackIndex = 0;
			
			Alert.show(sc_code + " - Created" );
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="SalesConformationEvent.ADD", properties="salesConformationvo")]
		public function add(salesConformationvo:VoSalesConformation):void
		{
			sh.executeServiceCall(salesConformationDetails.addNewSalesConformation(salesConformationvo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			var sc_code:String = event.result as String;
			
			if(model.saveCloseBtn)model.currentSellFormsIndex=0;
			
			model.scStackIndex = 0;
			
			Alert.show(sc_code + " - Created" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			getAll();
		}
		
		[EventHandler(event="SalesConformationEvent.UPDATE", properties="salesConformationvo")]
		public function update(salesConformationvo:VoSalesConformation):void
		{
			sh.executeServiceCall(salesConformationDetails.editSalesConformation(salesConformationvo), editResult, handleFault );	
		}
		
		private function editResult(event:ResultEvent):void
		{
			getAll();
			model.currentSellFormsIndex=0;	
			Alert.show( "SalesConfirmation Updated" );	
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="SalesConformationEvent.DELETE", properties="serialnumber")]
		public function deletef(serialnumber:Number):void
		{
			sh.executeServiceCall(salesConformationDetails.deleteSalesConformation(serialnumber),deleteResult, handleFault );	
		}
		
		private function deleteResult(event:ResultEvent):void
		{
			if(model.saveCloseBtn)
			{
				model.currentSellFormsIndex=0;
				Alert.show( "DRAFT - Deleted" );	
			}
			
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}